$(document).ready(function()
{
    var lbl = $('#pwdInfo');
    var txt = $('#txtPwd');

    var infoText = lbl.text();

    txt.blur(validate);
    txt.keyup(validate);
    txt.keyup(function(){ $('#result').fadeOut(1000)});
    $('#frmPwd').submit(function()
    {
        if (validate()) {
            $('#btnSubmit').fadeOut(300);
            $('#ajaxImg').fadeIn(500);

            $.ajax({
                type: 'POST',
                url: 'ajax_controller.php',
                data: {
                    requestType: 'function',
                    requestName: 'encodePassword',
                    requestParams: txt.val()
                },
                success: function(data) {
                    $('#result').fadeIn(1000);
                    $('#result>p').html(data);

                    $('#ajaxImg').hide(500, function(){
                        $('#btnSubmit').fadeIn(500);
                    })
                }
            });
        }

        return false;
    });

    function validate()
    {
        if (!/.+/.test(txt.val())) {
            lbl.addClass('error');
            lbl.text('↔ lỗi nhập liệu!');
            return false;
        }
        lbl.removeClass('error');
        lbl.text(infoText);
        return true;
    }
});